package com.hns.can.master.service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.master.entity.BaseLineInfoEntity;
import com.hns.can.master.entity.BaseStnInfoEntity;
import com.hns.can.master.entity.BaseStnLineMianEntity;
import com.hns.can.master.entity.BaseStnLineRelEntity;
import com.hns.can.master.vo.StnLineParamsVo;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.DataConvertUtil;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;

/**
 * 线路站点分配service
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Service
public class StnLineRelService extends CommonService {
    
    @Autowired
    private FillCodeDscValue codeDscValue;
    
	/**
	 * 据线路ID获取上行数据
	 * @param vo
	 * @return
	 * @author:lan
	 * @email:763836737@qq.com
	 */
    @SuppressWarnings("unchecked")
    public List<BaseStnInfoEntity> getUpStnLineRelByLineId(StnLineParamsVo vo) {
        List<BaseStnInfoEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("stnLineRel.getUpStnLineRelByLineId", vo), BaseStnInfoEntity.class);
        return list;
    }
    
    
    /**
     * 据线路ID获取下行数据
     * @param vo
     * @return
     * @author:lan
     * @email:763836737@qq.com
     */
    @SuppressWarnings("unchecked")
    public List<BaseStnInfoEntity> getDownStnLineRelByLineId(StnLineParamsVo  vo) {
        List<BaseStnInfoEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("stnLineRel.getDownStnLineRelByLineId", vo), BaseStnInfoEntity.class);
        return list;
    }
    
    /**
     * 未选站点
     * @param vo
     * @return
     * @author:lan
     * @email:763836737@qq.com
     */
    @SuppressWarnings("unchecked")
    public List<BaseStnInfoEntity> getUnSelStnLineRel(StnLineParamsVo vo) {
        List<BaseStnInfoEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("stnLineRel.getUnSelStnLineRel", vo), BaseStnInfoEntity.class);
        return list;
    }
    
    /**
     * 已选中站点(上行)
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @SuppressWarnings({"unchecked"})
    public List<BaseStnInfoEntity> getSelStnLineRel(StnLineParamsVo vo) throws Exception{
        List<BaseStnInfoEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("stnLineRel.getSelStnLineRel", vo), BaseStnLineRelEntity.class);
        return list;
    }
    
    /**
     * 已选中站点(下行)
     * @param vo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @SuppressWarnings({"unchecked"})
    public List<BaseStnInfoEntity> getSelStnLineRelDown(StnLineParamsVo vo) throws Exception{
        List<BaseStnInfoEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("stnLineRel.getSelStnLineRelDown", vo), BaseStnLineRelEntity.class);
        return list;
    }
    
    /**
     * 获取版本号
     * @param vo
     * @return
     * @author:lan
     * @email:763836737@qq.com
     */
    @SuppressWarnings("unchecked")
    public List<BaseStnLineRelEntity> getStrtusDate(StnLineParamsVo vo) {
        List<BaseStnLineRelEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("stnLineRel.getStrtusDate", vo), BaseStnLineRelEntity.class);
        return list;
    }
    
   /**
    * 保存站点线路关系数据
    * @param vo
    * @author:lan
    * @email:763836737@qq.com
    */
    @Transactional
    public void saveStnLineRel(StnLineParamsVo vo) {
        deleteStnLineRel(vo);
        //保存站点与线路主信息  
        BaseStnLineMianEntity saveBaseStnLineMianEntity= new BaseStnLineMianEntity();
        saveBaseStnLineMianEntity.setCorpId(vo.getCorpId());
        saveBaseStnLineMianEntity.setCrtTime(new Date());
        saveBaseStnLineMianEntity.setCrtUser(vo.getCrtUser());
        saveBaseStnLineMianEntity.setLineId(vo.getLineId());
        saveBaseStnLineMianEntity.setRelId(UtilString.getUUID());
        saveBaseStnLineMianEntity.setRemark(vo.getRemark());
        saveBaseStnLineMianEntity.setStrtusDate(vo.getStrtusDate());
        saveBaseStnLineMianEntity.setUpdateTime(new Date());
        saveBaseStnLineMianEntity.setUpdateUser(vo.getUpdateUser());
        commonEntityDao.save(saveBaseStnLineMianEntity);
        String relId=saveBaseStnLineMianEntity.getRelId();
        //上行、环形
        String[] stnIds=vo.getStnId().split(",");
        for(int i=0;i<stnIds.length;i++){
            BaseStnLineRelEntity entity = new BaseStnLineRelEntity();
            entity.setId(UtilString.getUUID());
            entity.setRelId(relId);
            entity.setCorpId(vo.getCorpId());
            entity.setLineType(vo.getStnType());
           // entity.setLineType("LIT00001");
            entity.setStnId(stnIds[i]);
            entity.setStnSn(new BigDecimal(i+1));
            entity.setCrtTime(new Date());   
            entity.setCrtUser(vo.getCrtUser());  
            entity.setUpdateTime(new Date());
            entity.setUpdateUser(vo.getCrtUser());
            commonEntityDao.save(entity);
        }
        //下行
        if(!"LIT00003".equals(vo.getStnType())){
            String[] stnIdsDown=vo.getStnIdDown().split(",");
            for(int i=0;i<stnIdsDown.length;i++){
                BaseStnLineRelEntity entity = new BaseStnLineRelEntity();
                entity.setId(UtilString.getUUID());
                entity.setRelId(relId);
                entity.setCorpId(vo.getCorpId());
                entity.setLineType("LIT00002");
                entity.setStnId(stnIdsDown[i]);
                entity.setStnSn(new BigDecimal(i+1));
                entity.setCrtTime(new Date());   
                entity.setCrtUser(vo.getCrtUser());  
                entity.setUpdateTime(new Date());
                entity.setUpdateUser(vo.getCrtUser());
                commonEntityDao.save(entity);
            }
        }
    }
    
  /**
   * 进入编辑页面 获取线路、类型、备注
   * @param vo
   * @return
   * @author:lan
   * @email:763836737@qq.com
   */
    @SuppressWarnings("unchecked")
    public List<BaseStnLineRelEntity> upUpdate(StnLineParamsVo vo) {
        List<BaseStnLineRelEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("stnLineRel.upUpdate", vo), BaseStnLineRelEntity.class);
        return list;
    }
    
    /**
     * 删除版本
     * @param vo
     * @author:lan
     * @email:763836737@qq.com
     */
    @SuppressWarnings({"rawtypes", "unchecked" })
    @Transactional
    public void deleteStnLineRel(StnLineParamsVo vo) {
        //查询站点与线路主信息 RELID  
        String relId =(String) sqlMapDao.selectOne("stnLineRel.deleteRel", vo);
        if(relId!=null){
            //删除站点与线路主信息
            BaseStnLineRelEntity deleteBaseStnLineRelEntity= new BaseStnLineRelEntity();
            deleteBaseStnLineRelEntity.setRelId(relId);
            Map mapRel = DataConvertUtil.ConvertObjToMap(deleteBaseStnLineRelEntity);
            commonEntityDao.deleteByParamsIgnoreNullValue(BaseStnLineRelEntity.class, mapRel);
            //删除站点与线路关系
            BaseStnLineMianEntity deleteBaseStnLineMianEntity = new BaseStnLineMianEntity();
            deleteBaseStnLineMianEntity.setLineId(vo.getLineId());
            deleteBaseStnLineMianEntity.setStrtusDate(vo.getStrtusDate());
            Map map = DataConvertUtil.ConvertObjToMap(deleteBaseStnLineMianEntity);
            commonEntityDao.deleteByParamsIgnoreNullValue(BaseStnLineMianEntity.class, map);
        }
    }
    
    /**
     * 是否存在相同版本
     * @param vo
     * @return
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public int checkVersion(StnLineParamsVo vo) {
        int count = (Integer) sqlMapDao.selectOne("stnLineRel.checkVersion", vo);
        return count;
    }
    
    /**
     * 根据线路id求线路信息
     * @param lineId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public BaseLineInfoEntity getLineInfo(String lineId) throws Exception {
        BaseLineInfoEntity entity = commonEntityDao.find(lineId, BaseLineInfoEntity.class);
        return entity;
    }
}
